package quiz;

import java.util.Date;

public class Result {


	/* ================ STATIC METHODS AND VARIABLES ================ */

	/**
	 * Function sets the DataBase object that will be referenced by all answers
	 * MUST BE CALLED BEFORE ANY ANSWERS ARE CREATED
	 * @param db - the DataBase object referenced by all accounts 
	 */
	public static void setDataBase(DataBase database) {
		db = database;
		db.open();
	}
	public static DataBase db;


	/* ================ STATIC METHODS AND VARIABLES ================ */

	/* ================ INSTANCE VARIABLES ================ */


	private int id;
	private Account user;
	private Quiz quiz;
	private int score;
	private boolean practice;
	private int time;
	private Date date_taken;

	/* ================ STATIC METHODS AND VARIABLES ================ */

	public Result(int userID, int quizID, int score, int time, boolean practice) {
		this.user = db.getAccount(userID);
		this.quiz = db.getQuiz(quizID);
		this.score = score;
		this.practice = practice;
		this.time = time;
		this.date_taken = new Date();
	}
	
	public Result(Account user, Quiz quiz, int score, int time, boolean practice) {
		this(user.getID(), quiz.getID(), score, time, practice);
	}

	/* ================ GETTERS ================ */

	/**
	 * Returns id of Result
	 * @return id of Result
	 */
	public int getID() {
		return id;
	}

	/**
	 * Returns id of user Result is associated with
	 * @return int id of user
	 */
	public Account getUser() {
		return user;
	}

	public Quiz getQuiz() {
		return quiz;
	}

	public int getScore() {
		return Math.abs(score);
	}

	public int getTime() {
		return time;
	}

	public Date getDate_taken() {
		return date_taken;
	}
	
	public boolean wasPractice() {
		return practice;
	}



	/* ================ GETTERS ================ */

	/* ================ SETTERS ================ */



	public void setID(int id) {
		this.id = id;
	}

	public void setUserID(int userID) {
		this.user = db.getAccount(userID);
	}
	
	public void setUserID(Account user) {
		this.user = user;
	}

	public void setQuiz(int quizID) {
		this.quiz = db.getQuiz(quizID);
	}
	
	public void setQuiz(Quiz quiz) {
		this.quiz = quiz;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public void setDate_taken(Date date_taken) {
		this.date_taken = date_taken;
	}



	/* ================ SETTERS ================ */


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + quiz.getID();
		result = prime * result + score;
		result = prime * result + time;
		result = prime * result + user.getID();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Result other = (Result) obj;
		if (id != other.id)
			return false;
		if (quiz.getID() != other.quiz.getID())
			return false;
		if (score != other.score)
			return false;
		if (time != other.time)
			return false;
		if (user.getID() != other.user.getID())
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Result [id=" + id + ", userID=" + user.getID() + ", quizID=" + quiz.getID()
				+ ", score=" + score + ", time=" + time + ", date_taken="
				+ date_taken + "]";
	}



}
